package com.yinshan.arface;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.ThumbnailUtils;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * @author limh
 * @function 取景框
 * @date 2019/7/18 17:59
 */
public class ViewfinderView extends View {

    private Bitmap bitmap;
    private Rect bitRect;
    private Rect bitShow;
    private Paint paint;

    //识别框离顶部的高度(dp)
    private int marginTop = 120;

    public ViewfinderView(Context context) {
        super(context);
        init(context, null);
    }

    public ViewfinderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ViewfinderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        DisplayMetrics dm1 = getResources().getDisplayMetrics();
        int screenWidth = dm1.widthPixels;
        //识别框之外的背景色
        int bgColor;
        //识别框自身高度
        int viewWidth;
        int viewHeight;
        int bgImage = R.drawable.ic_face_idbox;
        if (null != attrs) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ViewfinderView);
            bgColor = ta.getColor(R.styleable.ViewfinderView_vf_bgColor, ContextCompat.getColor(context, R.color.viewfinder_mask));
            marginTop = ta.getDimensionPixelSize(R.styleable.ViewfinderView_vf_marginTop, 120);
            viewWidth = ta.getDimensionPixelSize(R.styleable.ViewfinderView_vf_viewWidth, 494);
            viewHeight = ta.getDimensionPixelSize(R.styleable.ViewfinderView_vf_viewHeight, 494);
            bgImage = ta.getResourceId(R.styleable.ViewfinderView_vf_viewImage, R.drawable.ic_face_idbox);
            ta.recycle();
        } else {
            bgColor = ContextCompat.getColor(context, R.color.viewfinder_mask);
            viewWidth = screenWidth * 3 / 4;
            viewHeight = screenWidth * 3 / 4;
        }

        int left = (screenWidth - viewWidth) / 2;
        int top = marginTop;
        int right = left + viewWidth;
        int bottom = top + viewWidth;
        bitRect = new Rect(0, 0, viewWidth, viewHeight);
        bitShow = new Rect(left, top, right, bottom);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(bgColor);
        Bitmap tmp = BitmapFactory.decodeResource(context.getResources(), bgImage);
        bitmap = ThumbnailUtils.extractThumbnail(tmp, viewWidth, viewHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null != bitmap) {
            canvas.drawBitmap(bitmap, bitRect, bitShow, paint);
            int width = getWidth();
            int height = getHeight();
            canvas.drawRect(0, 0, width, bitShow.top, paint);// Rect_1
            canvas.drawRect(0, bitShow.top, bitShow.left, bitShow.bottom + 1, paint);
            canvas.drawRect(bitShow.right + 1, bitShow.top, width, bitShow.bottom + 1, paint);
            canvas.drawRect(0, bitShow.bottom + 1, width, height, paint);
        }
    }

}
